package org.jeecg.modules.student.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.SysUserTit;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.student.entity.AstuInformation;
import org.jeecg.modules.student.service.IAstuInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 学生信息表
 * @Author: jeecg-boot
 * @Date:   2020-11-06
 * @Version: V1.0
 */
@Api(tags="学生信息表")
@RestController
@RequestMapping("/student/astuInformation")
@Slf4j
public class AstuInformationController extends JeecgController<AstuInformation, IAstuInformationService> {
	@Autowired
	private IAstuInformationService astuInformationService;

	/**
	 * 分页列表查询
	 *
	 * @param astuInformation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "学生信息表-分页列表查询")
	@ApiOperation(value="学生信息表-分页列表查询", notes="学生信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AstuInformation astuInformation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AstuInformation> queryWrapper = QueryGenerator.initQueryWrapper(astuInformation, req.getParameterMap());
		Page<AstuInformation> page = new Page<AstuInformation>(pageNo, pageSize);
		IPage<AstuInformation> pageList = astuInformationService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param astuInformation
	 * @return
	 */
	@AutoLog(value = "学生信息表-添加")
	@ApiOperation(value="学生信息表-添加", notes="学生信息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AstuInformation astuInformation) {
		astuInformationService.save(astuInformation);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param student
	 * @return
	 */
	@AutoLog(value = "学生信息表-编辑")
	@ApiOperation(value="学生信息表-编辑", notes="学生信息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AstuInformation student) {
		System.out.println("前端收到的数据信息=====》"+student.toString());
		//修改学生信息
		astuInformationService.updateStudentInfo(student);
		//修改用户信息
		if(student.getUserId().toString().equals("")){
			return Result.error(500, "修改失败");
		}else{
			SysUserTit user = astuInformationService.selectUser(student.getUserId());
			// 设置用户姓名
			user.setRealname(student.getStuName());
			//设置用户姓名
			if (student.getStuSex().equals("男")){
				user.setSex(1);
			}else {
				user.setSex(2);
			}
			astuInformationService.updateUserInfo(user);
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "学生信息表-通过id删除")
	@ApiOperation(value="学生信息表-通过id删除", notes="学生信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		//	在删除之前先获得student的userid
		AstuInformation student = astuInformationService.getById(id);
		String userId = student.getUserId();
		//	删除学生信息
		astuInformationService.removeById(id);
		//删除用户信息
		int info = astuInformationService.deleteUserById(userId);
		if (info>0){
			return Result.OK("删除成功!");
		}else {
			return Result.OK("删除失败!");
		}
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "学生信息表-批量删除")
	@ApiOperation(value="学生信息表-批量删除", notes="学生信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		//删除用户
		List<String> list = Arrays.asList(ids.split(","));
		for (String studentid : list) {
			// 这里的id是student 的id ，而不是userid
			//得到student 的userid
			String userId = astuInformationService.getById(studentid).getUserId();
			astuInformationService.deleteUserById(userId);
		}
		this.astuInformationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "学生信息表-通过id查询")
	@ApiOperation(value="学生信息表-通过id查询", notes="学生信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AstuInformation astuInformation = astuInformationService.getById(id);
		if(astuInformation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(astuInformation);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param astuInformation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AstuInformation astuInformation) {
        return super.exportXls(request, astuInformation, AstuInformation.class, "学生信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AstuInformation.class);
    }

}
